package com.example.leetcode;

import java.util.Arrays;
import java.util.List;

/**
 * 通过删除字母匹配到字典里最长单词
 * 双指针 目的匹配子序列
 * 储存子序列
 */
public class Demo0524 {

    public static void main(String[] args) {
        String s="dfbfbsrtrtbs";
        List<String> dictionary = Arrays.asList("abf","dfb","rrtr","bfbrt");
        findLongestWord(s,dictionary);
    }

    public static String findLongestWord(String s, List<String> dictionary) {
        String res="";
        for (String s1 : dictionary) {
            int j=0;
            for (int i = 0; i < s.length(); i++) {
                if (s.charAt(i)==s1.charAt(j)){
                    j++;
                }
                if (j==s1.length()){
                    res = (j>res.length() || (j==res.length() && s1.compareTo(res)<0))?s1:res;
                    break;
                }
            }
        }
        return res;
    }
}
